//
//  DYZixunInputView.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/6.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit

class DYZixunInputView: SYBaseView {

    var model: DYZixunModel!
    var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor.white
        
        return v
    }()
    
    let nameLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x111111)
        v.font = 17.autoFontSize()
        
        return v
    }()
    
    let placeholdLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x999999)
        v.font = 13.autoFontSize()
        v.text = "请写下您的问题"
        return v
    }()
    
    let numLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x999999)
        v.font = 13.autoFontSize()
        v.text = "0/200"
        return v
    }()
    
    let pic: UIImageView = {
        let v: UIImageView = UIImageView()
        
        v.layer.cornerRadius = 3
        return v
    }()
    
    let chakan: UIButton = {
        let v: UIButton  = UIButton()
        v.setTitle("详情 >", for: .normal)
        v.setTitleColor(UIColor(hex: 0x999999), for: .normal)
        v.titleLabel?.font = 13.autoFontSize()
        return v
    }()
    
    var sepView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = Constant.bgViewColor
        return v
    }()
    
    var textView: UITextView = {
        let v: UITextView = UITextView()
        v.font = 13.autoFontSize()
//        v.backgroundColor = .blue
        v.backgroundColor = .clear
        return v
    }()
    
    func configure(_ model: DYZixunModel) {
        self.model = model
        nameLb.text = model.name
        pic.sy_setWithUrl(model.imgUrl)
    }
    
    override func initViews() {
        
        //当文本框内容改变时，将内容输出到控制台上
        textView.rx.text.orEmpty.asObservable()
            .subscribe(onNext: {
                print("您输入的是：\($0)")
                if $0 == "" {
                    self.placeholdLb.isHidden = false
                } else {
                    self.placeholdLb.isHidden = true
                }
                if $0.count > 200 {
                    let substr = self.textView.text.prefix(200)
                    self.textView.text = String(substr)
                }
                
            })
            .disposed(by: disposeBag)
        
        //开始编辑响应
        textView.rx.didBeginEditing
            .subscribe(onNext: {
                print("开始编辑")
                
            })
            .disposed(by: disposeBag)
        //结束编辑响应
        textView.rx.didEndEditing
            .subscribe(onNext: {
                print("结束编辑")
                
            })
            .disposed(by: disposeBag)
        
        self.backgroundColor = Constant.bgViewColor
        chakan.addAction {[weak self] in
            if let `self` = self {
                let vc = DYJingquDetailViewController(self.model.id ?? "")
                self.currentVC.sy_push(vc)
            }
        }
        setSearchView()
        addSubview(bgView)
        addSubview(nameLb)
        addSubview(placeholdLb)
        addSubview(numLb)
        addSubview(pic)
        addSubview(chakan)
        addSubview(sepView)
        addSubview(textView)
        bgView.snp.makeConstraints { (make) in
            make.top.equalTo(self)
            make.left.right.equalToSuperview()
            make.height.equalTo(246.0.autoWidth())
        }
        nameLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(pic)
            make.left.equalTo(pic.snp.right).offset(10.5.autoWidth())
            make.height.equalTo(20.0.autoWidth())
        }
        placeholdLb.snp.makeConstraints { (make) in
            make.top.equalTo(self.sepView.snp.bottom).offset(14.5.autoWidth())
            make.left.equalToSuperview().offset(15.5.autoWidth())
            make.height.equalTo(15.0.autoWidth())
        }
        numLb.snp.makeConstraints { (make) in
            make.bottom.equalTo(self.bgView).offset(-13.5.autoWidth())
            make.right.equalToSuperview().offset(-15.5.autoWidth())
            make.height.equalTo(15.0.autoWidth())
        }
        pic.snp.makeConstraints { (make) in
            make.top.equalTo(self).offset(10.5.autoWidth())
            make.left.equalToSuperview().offset(15.5.autoWidth())
            make.height.width.equalTo(66.0.autoWidth())
        }
        chakan.snp.makeConstraints { (make) in
            make.centerY.equalTo(pic)
            make.right.equalTo(self).offset(-15.5.autoWidth())
            make.height.equalTo(20.0.autoWidth())
        }
        sepView.snp.makeConstraints { (make) in
            make.top.equalTo(self.pic.snp.bottom).offset(10.5.autoWidth())
            make.left.right.equalToSuperview()
            make.height.equalTo(10.0.autoWidth())
        }
        textView.snp.makeConstraints { (make) in
            make.top.equalTo(self.sepView.snp.bottom).offset(6.5.autoWidth())
            make.left.equalToSuperview().offset(12.5.autoWidth())
            make.right.equalToSuperview().offset(-12.5.autoWidth())
            make.bottom.equalTo(bgView).offset(-30.autoWidth())
        }
    }

    func setSearchView() {
        
        let zixun: UIButton = UIButton()
        zixun.addTarget(self, action: #selector(tijiaoAction), for: .touchUpInside)
        zixun.backgroundColor = UIColor(hex: 0x367FF6)
        zixun.setTitle("发布", for: .normal)
        zixun.setTitleColor(UIColor.white, for: .normal)
        zixun.titleLabel?.font = 18.autoFontSize()
        zixun.layer.cornerRadius = 5
        self.addSubview(zixun)
        zixun.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(270.5.autoWidth())
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.left.equalToSuperview().offset(autoWidth(15.0))
            make.height.equalTo(44.autoWidth())
        }
    }
    
    @objc func tijiaoAction() {
        self.clickedClosure?(0)
    }
}
